package apicmd

import (
	"emsctrl/db"
	"fmt"
)

type Energy_storage_para struct {
	con_efficiency float32
}

type Price_cfg_mng struct {
	PriceTable []Price_cfg_table `json:"price_table"`
}

type Price_cfg_table struct {
	Price_data
	Time []Price_time_table `json:"time"`
}
type Price_data struct {
	StartDay string `json:"start_day"`
	EndTDay  string `json:"end_day"`
}

type Price_time_table struct {
	Price      float32 `json:"price"`
	StartTime  string  `json:"start_time"`
	EndTime    string  `json:"end_time"`
	StateVal   int     `json:"state_val"`
	EndVal     int     `json:"end_val"`
	TotolPrice float32 `json:"totol_price"`
	Mode       int     `json:"mode"`
}

type Load_table struct {
	StartVal int     `json:"start_val"`
	EndVal   int     `json:"end_val"`
	Power    float32 `json:"power"`
}

type Grid_table struct {
	StartVal int     `json:"start_val"`
	EndVal   int     `json:"end_val"`
	Power    float32 `json:"power"`
}

type Bms_table struct {
	Soc        float32 `json:"soc"`
	Capability float32 `json:"capability"` //kwh
}

func GetDevice() {
	basics := []db.DeviceBasics{}
	db.WhereFind("created_at > '2024-09-09 00:00:00' AND created_at < '2024-09-09 14:00:00'", &basics)
	for i, basic := range basics {
		fmt.Println(i, basic.Data)
	}
	grid := []db.GridPower{}
	db.WhereFind("created_at > '2024-09-09 00:00:00' AND created_at < '2024-09-09 14:00:00'", &grid)
	for i, a := range grid {
		fmt.Println(i, a.Power.Power)
	}
	bms := []db.BmsPower{}
	db.WhereFind("created_at > '2024-09-09 00:00:00' AND created_at < '2024-09-09 14:00:00'", &bms)
	for i, b := range bms {
		fmt.Println(i, b.Power.Power)
	}
}

func GetGridMeter_Power() {
	basics := []db.DeviceBasics{}
	sprintf := fmt.Sprintf(`from(bucket:"%v")
	|> range(start: -%vs)
	|> filter(fn: (r) =>
	r._measurement == "gridmeter" and
	r._field == "%v"
	)
	|> mean()`, db.INFLUXDB_DATABASE, 5, "T12")
	db.WhereFind(sprintf, basics)
}

var g_priceCfg []Price_cfg_table

func PriceInit() {

}

func PolicyGen() {

}
